﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc;
using System.IO;

namespace Presentation.Web.Admin.Controllers
{
    [HandleError]
    public class PicController : Infrastructure.Crosscutting.NetFramework.Controllers.BaseController
    {
        Application.Admin.Pic.IPicInfoClassService picInfoClassService;

        Application.Admin.Pic.IPicClassService picClassService;

        Application.Admin.Pic.IPicThumbSizeService picThumbSizeService;

        Application.Admin.Pic.IPicThumbService picThumbService;

        Application.Admin.Pic.IPicInfoService picInfoService;

        Application.Admin.Goods.IGoodsPicService goodsPicService;

        Application.Admin.Sight.ISightPointPicService sightPicService;

        Application.Admin.Tour.ITourPicService tourPicService;

        Application.Admin.Article.IArticlePicService articlePicService;

        Application.Admin.Advertise.IAdvertisePicService adPicService;

        public PicController(Infrastructure.Crosscutting.NetFramework.IWorkContext work,
            Application.Admin.Pic.IPicInfoClassService picInfoClass,
            Application.Admin.Pic.IPicClassService picClass,
            Application.Admin.Pic.IPicThumbSizeService picThumbSize,
            Application.Admin.Pic.IPicInfoService picInfo,
            Application.Admin.Pic.IPicThumbService picThumb,
            Application.Admin.Goods.IGoodsPicService goodsPic,
            Application.Admin.Sight.ISightPointPicService sightPic,
            Application.Admin.Tour.ITourPicService tourPic,
            Application.Admin.Article.IArticlePicService articlePic,
            Application.Admin.Advertise.IAdvertisePicService adPic)
            : base(work)
        {
            if (picInfoClass == null)
            {
                throw new ArgumentNullException("picInfoClassService is null");
            }
            if (picClass == null)
            {
                throw new ArgumentNullException("picClassService is null");
            }
            if (picThumbSize == null)
            {
                throw new ArgumentNullException("picClassService is null");
            }
            if (picInfo == null)
            {
                throw new ArgumentNullException("picInfoService is null");
            }
            if (picThumb == null)
            {
                throw new ArgumentNullException("picThumbService is null");
            }

            if (goodsPic == null)
            {
                throw new ArgumentNullException("goodsPicService is null");
            }
            if (sightPic == null)
            {
                throw new ArgumentNullException("sightPicService is null");
            }
            if (tourPic == null)
            {
                throw new ArgumentNullException("tourPicService is null");
            }

            if (articlePic == null)
            {
                throw new ArgumentNullException("articlePicService is null");
            }
            if (adPic == null)
            {
                throw new ArgumentNullException("adPicService is null");
            }
            picInfoClassService = picInfoClass;
            picClassService = picClass;
            picThumbSizeService = picThumbSize;
            picInfoService = picInfo;
            picThumbService = picThumb;
            goodsPicService = goodsPic;
            sightPicService = sightPic;
            tourPicService = tourPic;
            articlePicService = articlePic;
            adPicService = adPic;
        }

        #region pic

        public ViewResult Index()
        {
            var data = this.GetPicTrueState();
            return View(data.ToDto());
        }

        [GridAction]
        public JsonResult AjaxIndex(int? targetId, string type, string searchKey)
        {
            var data = this.GetPicTrueState();
            if (!string.IsNullOrEmpty(type) && targetId.HasValue)
            {
                var tempId = (int)targetId;
                if (tempId > 0)
                {
                    data = this.GetPicTrueState(type, data, tempId);
                }
            }
            if (!string.IsNullOrEmpty(searchKey))
            {
                data = data.Where(e => e.SourceName.Contains(searchKey));
            }
            var dto = data.ToDto();
            var model = new GridModel<Domain.Dto.PicInfoDto>
            {
                Data = dto,
                Total = dto.Count()
            };
            return new JsonResult { Data = model, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        [GridAction]
        [HttpPost]
        public JsonResult Delete(int id, int targetId, string type)
        {
            var delete = picInfoService.Get(id);
            if (delete != null)
            {
                picInfoService.DeleteTrue(delete, this.WorkContext.CurrentUser);
            }
            var data = this.GetPicTrueState();
            if (!string.IsNullOrEmpty(type) && targetId > 0)
            {
                data = this.GetPicTrueState(type, data, targetId);
                this.PicDeleteTrueBase(id, targetId, type);
            }
            return Json(data.ToDto(), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult MulDelete(FormCollection del)
        {
            var res = false;
            var message = "";
            var selectedList = del.GetValues("selectRow");
            //del
            if (selectedList != null && selectedList.Count() > 0)
            {
                var delList = selectedList.ToIntList();
                res = picInfoService.DeleteTrue(delList, this.WorkContext.CurrentUser);
                message = res == true ? "" : "删除失败了，T_T";
                var target = del.GetValue("targetId").AttemptedValue;
                var type = del.GetValue("type").AttemptedValue;
                var targetId = 0;
                int.TryParse(target, out targetId);
                if (!string.IsNullOrEmpty(type) && targetId > 0)
                {
                    if (type.CompareTo("景点") == 0)
                    {
                        var sightPicDel = sightPicService.GetList().Where(e => e.SightId == targetId &&
                                   delList.Contains(e.PicInfoId)).Select(e => e.Id);
                        sightPicService.DeleteTrue(sightPicDel.ToList(), this.WorkContext.CurrentUser);
                    }
                    else if (type.CompareTo("线路") == 0)
                    {
                        var tourPicDel = tourPicService.GetList().Where(e => e.TourId == targetId &&
                                   delList.Contains(e.PicInfoId)).Select(e => e.Id);
                        tourPicService.DeleteTrue(tourPicDel.ToList(), this.WorkContext.CurrentUser);
                    }
                    else if (type.CompareTo("商品") == 0)
                    {
                        var tourPicDel = goodsPicService.GetList().Where(e => e.GoodsId == targetId &&
                                   delList.Contains(e.PicInfoId)).Select(e => e.Id);
                        goodsPicService.DeleteTrue(tourPicDel.ToList(), this.WorkContext.CurrentUser);
                    }
                }
            }
            //返回空，是为了不再请求数据，因为他自己会重新binding的
            return Json(new { success = res, message = message }, JsonRequestBehavior.AllowGet);
        }

        [GridAction]
        [HttpPost]
        public JsonResult Edit(Domain.Dto.PicInfoDto dto, int targetId, string type)
        {
            if (dto.Id > 0)
            {
                var entity = picInfoService.Get(dto.Id);
                if (entity != null && entity.Id > 0)
                {
                    //entity.Name = dto.Name;
                    entity.Remark = dto.Remark;
                    entity.SourceName = dto.SourceName;
                    entity.SortNum = dto.SortNum;
                    picInfoService.Modify(entity, this.WorkContext.CurrentUser);
                }
                if (!string.IsNullOrEmpty(type) && type == "景点")
                {
                    var sightPic = sightPicService.GetList().Where(e => e.SightId == targetId && e.PicInfoId == dto.Id).FirstOrDefault();
                    if (sightPic != null)
                    {
                        sightPic.SortNum = dto.SortNum;
                        sightPicService.Modify(sightPic, this.WorkContext.CurrentUser);
                    }
                }
            }
            var data = this.GetPicTrueState();
            if (!string.IsNullOrEmpty(type) && targetId > 0)
            {
                data = this.GetPicTrueState(type, data, targetId);
            }
            var model = new GridModel<Domain.Dto.PicInfoDto>
            {
                Data = data.ToDto(),
                Total = data.Count()
            };
            return new JsonResult { Data = model, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }


        [HttpGet]
        public JsonResult Search(int targetId, string type, string term)
        {
            var all = this.GetPicTrueState();
            var data = this.GetPicTrueState(type, all, targetId)
                   .Where(e => e.SourceName.Contains(term.Trim()))
                           .OrderBy(e => e.Id).Take(10)
                           .Select(e => e.SourceName).Distinct();
            return Json(data.ToList(), JsonRequestBehavior.AllowGet);
        }


        #endregion

        #region pic class


        public ActionResult PicClassIndex()
        {
            var data = this.GetPicClassTrueState();
            return View(data.ToDto());
        }

        [GridAction]
        public JsonResult AjaxPicClassIndex(string searchKey)
        {
            var data = this.GetPicClassTrueState();
            if (!string.IsNullOrEmpty(searchKey))
            {
                data = data.Where(e => e.Name.Contains(searchKey));
            }
            var dto = data.ToDto();
            var model = new GridModel<Domain.Dto.PicClassDto>
            {
                Data = dto,
                Total = dto.Count()
            };
            return new JsonResult { Data = model, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        [GridAction]
        [HttpPost]
        public JsonResult AddPicClass(Domain.Dto.PicClassDto dto)
        {
            dto.AddTime = System.DateTime.Now;
            dto.IpAddress = Infrastructure.Crosscutting.Comm.Service.StringService.GetRealIP();
            dto.AudTime = null;
            dto.DeleteTime = null;
            dto.IsAud = true;
            dto.IsDelete = false;
            dto.IsTop = false;
            dto.IsVouch = false;
            dto.SortNum = 0;
            dto.TopTime = null;
            dto.UserName = this.WorkContext.CurrentUser.UserName;
            dto.UserId = this.WorkContext.CurrentUser.Id;
            dto.VouchTime = null;

            var entity = dto.ToEntity();
            picClassService.Add(entity, this.WorkContext.CurrentUser);
            var data = this.GetPicClassTrueState();
            var model = new GridModel<Domain.Dto.PicClassDto>
            {
                Data = data.ToDto(),
                Total = data.Count()
            };
            return new JsonResult { Data = model, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        [GridAction]
        [HttpPost]
        public JsonResult DeletePicClass(int id)
        {
            var delete = picClassService.Get(id);
            if (delete != null)
            {
                picClassService.DeleteTrue(delete, this.WorkContext.CurrentUser);
            }
            var data = this.GetPicClassTrueState().ToDto();
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        [GridAction]
        [HttpPost]
        public JsonResult MulDeletePicClass(FormCollection del)
        {
            var res = false;
            var message = "";
            var selectedList = del.GetValues("selectRow");
            if (selectedList != null && selectedList.Count() > 0)
            {
                res = picClassService.DeleteTrue(selectedList.ToIntList(), this.WorkContext.CurrentUser);
                message = res == true ? "" : "删除失败了，T_T";
            }
            //返回空，是为了不再请求数据，因为他自己会重新binding的
            return Json(new { success = res, message = message }, JsonRequestBehavior.AllowGet);
        }

        [GridAction]
        [HttpPost]
        public JsonResult EditPicClass(Domain.Dto.PicClassDto dto)
        {
            if (dto.Id > 0)
            {
                var entity = picClassService.Get(dto.Id);
                if (entity != null && entity.Id > 0)
                {
                    entity.Name = dto.Name;
                    entity.Remark = dto.Remark;
                    picClassService.Modify(entity, this.WorkContext.CurrentUser);
                }
            }
            var data = this.GetPicClassTrueState();
            var model = new GridModel<Domain.Dto.PicClassDto>
            {
                Data = data.ToDto(),
                Total = data.Count()
            };
            return new JsonResult { Data = model, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }


        [HttpGet]
        public JsonResult SearchPicClass(string term)
        {
            if (!string.IsNullOrEmpty(term))
            {
                var data = this.GetPicClassTrueState().Where(e => e.Name.Contains(term.Trim()))
                            .OrderBy(e => e.Id).Take(10)
                            .Select(e => e.Name).Distinct();
                return Json(data.ToList(), JsonRequestBehavior.AllowGet);
            }
            else
            {
                var data = this.GetPicClassTrueState().OrderBy(e => e.Id).Take(10)
                            .Select(e => e.Name).Distinct();
                return Json(data.ToList(), JsonRequestBehavior.AllowGet);
            }
        }

        #endregion

        #region upload pic

        public ViewResult Upload()
        {
            return View();
        }

        public ActionResult UploadAdvertise(IEnumerable<HttpPostedFileBase> advertiseFiles,
            int advertiseId, string returnUrl, int selected,
            string picType, //景点，商品，线路等图片分类
            IEnumerable<int> picThumbSize)//生成缩略图类型id哦
        {
            UploadPicBase(advertiseFiles, picType, picThumbSize, advertiseId);
            if (!string.IsNullOrEmpty(returnUrl))
            {
                if (selected > 0)
                {
                    returnUrl += (returnUrl.IndexOf("?") > 0 ? "&" : "?") +
                        Infrastructure.Crosscutting.Comm.Service.ConstService.UrlParaSelected + "=" + selected;
                }
                return Redirect(returnUrl);
            }
            return RedirectToAction("index", new { selected = selected });
        }

        public ActionResult UploadArticle(IEnumerable<HttpPostedFileBase> articleFiles,
            int articleId, string returnUrl, int selected,
            string picType, //景点，商品，线路等图片分类
            IEnumerable<int> picThumbSize)//生成缩略图类型id哦
        {
            UploadPicBase(articleFiles, picType, picThumbSize, articleId);
            if (!string.IsNullOrEmpty(returnUrl))
            {
                if (selected > 0)
                {
                    returnUrl += (returnUrl.IndexOf("?") > 0 ? "&" : "?") +
                        Infrastructure.Crosscutting.Comm.Service.ConstService.UrlParaSelected + "=" + selected;
                }
                return Redirect(returnUrl);
            }
            return RedirectToAction("index", new { selected = selected });
        }

        public ActionResult UploadSight(IEnumerable<HttpPostedFileBase> sightFiles,
            int sightId, string returnUrl, int selected,
            string picType, //景点，商品，线路等图片分类
            IEnumerable<int> picThumbSize)//生成缩略图类型id哦
        {
            UploadPicBase(sightFiles, picType, picThumbSize, sightId);
            if (!string.IsNullOrEmpty(returnUrl))
            {
                if (selected > 0)
                {
                    returnUrl += (returnUrl.IndexOf("?") > 0 ? "&" : "?") +
                        Infrastructure.Crosscutting.Comm.Service.ConstService.UrlParaSelected + "=" + selected;
                }
                return Redirect(returnUrl);
            }
            return RedirectToAction("index", new { selected = selected });
        }

        public ActionResult UploadTour(IEnumerable<HttpPostedFileBase> tourFiles,
            int tourId, string returnUrl, int selected,
            string picType, //景点，商品，线路等图片分类
            IEnumerable<int> picThumbSize)//生成缩略图类型id哦
        {
            UploadPicBase(tourFiles, picType, picThumbSize, tourId);
            if (!string.IsNullOrEmpty(returnUrl))
            {
                if (selected > 0)
                {
                    returnUrl += (returnUrl.IndexOf("?") > 0 ? "&" : "?") +
                        Infrastructure.Crosscutting.Comm.Service.ConstService.UrlParaSelected + "=" + selected;
                }
                return Redirect(returnUrl);
            }
            return RedirectToAction("index", new { selected = 1 });
        }

        public ActionResult UploadGoods(IEnumerable<HttpPostedFileBase> goodsFiles,
            int goodsId, string returnUrl, int selected,
            string picType, //景点，商品，线路等图片分类
            IEnumerable<int> picThumbSize)//生成缩略图类型id哦
        {
            UploadPicBase(goodsFiles, picType, picThumbSize, goodsId);
            if (!string.IsNullOrEmpty(returnUrl))
            {
                if (selected > 0)
                {
                    returnUrl += (returnUrl.IndexOf("?") > 0 ? "&" : "?") +
                        Infrastructure.Crosscutting.Comm.Service.ConstService.UrlParaSelected + "=" + selected;
                }
                return Redirect(returnUrl);
            }
            return RedirectToAction("index", new { selected = 2 });
        }

        #endregion

        #region pic thumb size

        public ActionResult PicThumbSizeIndex()
        {
            var data = this.GetPicThumbSizeTrueState();
            return View(data.ToDto());
        }

        [GridAction]
        public JsonResult AjaxPicThumbSizeIndex()
        {
            var data = this.GetPicThumbSizeTrueState();
            var dto = data.ToDto();
            var model = new GridModel<Domain.Dto.PicThumbSizeDto>
            {
                Data = dto,
                Total = dto.Count()
            };
            return new JsonResult { Data = model, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        [GridAction]
        [HttpPost]
        public JsonResult AddPicThumbSize(Domain.Dto.PicThumbSizeDto dto)
        {
            dto.AddTime = System.DateTime.Now;
            dto.IpAddress = Infrastructure.Crosscutting.Comm.Service.StringService.GetRealIP();
            dto.State = true;
            dto.SortNum = 0;
            var entity = dto.ToEntity();
            picThumbSizeService.Add(entity, this.WorkContext.CurrentUser);
            var data = this.GetPicThumbSizeTrueState();
            var model = new GridModel<Domain.Dto.PicThumbSizeDto>
            {
                Data = data.ToDto(),
                Total = data.Count()
            };
            return new JsonResult { Data = model, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        [GridAction]
        [HttpPost]
        public JsonResult DeletePicThumbSize(int id)
        {
            var delete = picThumbSizeService.Get(id);
            if (delete != null)
            {
                picThumbSizeService.DeleteTrue(delete, this.WorkContext.CurrentUser);
            }
            var data = this.GetPicThumbSizeTrueState().ToDto();
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        [GridAction]
        [HttpPost]
        public JsonResult MulDeletePicThumbSize(FormCollection del)
        {
            var res = false;
            var message = "";
            var selectedList = del.GetValues("selectRow");
            if (selectedList != null && selectedList.Count() > 0)
            {
                res = picThumbSizeService.DeleteTrue(selectedList.ToIntList(), this.WorkContext.CurrentUser);
                message = res == true ? "" : "删除失败了，T_T";
            }
            //返回空，是为了不再请求数据，因为他自己会重新binding的
            return Json(new { success = res, message = message }, JsonRequestBehavior.AllowGet);
        }

        [GridAction]
        [HttpPost]
        public JsonResult EditPicThumbSize(Domain.Dto.PicThumbSizeDto dto)
        {
            if (dto.Id > 0)
            {
                var entity = picThumbSizeService.Get(dto.Id);
                if (entity != null && entity.Id > 0)
                {
                    entity.ThumbHeight = dto.ThumbHeight;
                    entity.ThumbWidth = dto.ThumbWidth;
                    entity.Name = dto.Name;
                    entity.Remark = dto.Remark;
                    picThumbSizeService.Modify(entity, this.WorkContext.CurrentUser);
                }
            }
            var data = this.GetPicThumbSizeTrueState();
            var model = new GridModel<Domain.Dto.PicThumbSizeDto>
            {
                Data = data.ToDto(),
                Total = data.Count()
            };
            return new JsonResult { Data = model, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        #endregion

        protected void UploadPicBase(IEnumerable<HttpPostedFileBase> attachments, string picType, IEnumerable<int> picThumbSize, int targetId)
        {
            var res = false;
            var message = "";
            var mapPath = this.GetPicSaveMapPath(picType);
            try
            {
                foreach (var item in attachments)
                {
                    //filename=123.jpg
                    var sourceFileName = Path.GetFileName(item.FileName);
                    var fileSuffix = Path.GetExtension(sourceFileName);
                    //14646546546546.jpg
                    var newName = System.DateTime.Now.ToFileTime() + fileSuffix;
                    // 物理路径+14646546546546.jpg
                    var sourcePhysicalPath = Path.Combine(Server.MapPath(mapPath), newName);

                    //保存原图
                    item.SaveAs(sourcePhysicalPath);

                    //添加一条到数据库
                    #region add to database

                    Domain.Entity.PicInfo picInfo = new Domain.Entity.PicInfo();
                    picInfo.AddTime = System.DateTime.Now;
                    picInfo.AudTime = null;
                    picInfo.DeleteTime = null;
                    picInfo.Height = 0;
                    //picInfo.Id = 0;
                    picInfo.IpAddress = Infrastructure.Crosscutting.Comm.Service.StringService.GetRealIP();
                    picInfo.IsAud = true;
                    picInfo.IsDelete = false;
                    picInfo.IsTop = false;
                    picInfo.IsVouch = false;

                    picInfo.Name = newName;
                    picInfo.PhyPrePath = mapPath.Substring(1);
                    picInfo.Remark = Path.GetFileNameWithoutExtension(sourceFileName);

                    picInfo.SortNum = 0;
                    picInfo.Source = "官网";

                    picInfo.SourceName = sourceFileName;

                    picInfo.State = true;
                    picInfo.TopTime = null;
                    picInfo.UserId = this.WorkContext.CurrentUser.Id;
                    picInfo.UserName = this.WorkContext.CurrentUser.UserName;
                    picInfo.ViewCount = 0;
                    picInfo.VouchTime = null;


                    var webFullPath = mapPath.Substring(1) + newName;
                    picInfo.WebFullPath = webFullPath;
                    picInfo.WebPrePath = mapPath.Substring(1);
                    picInfo.Width = 0;

                    picInfoService.Add(picInfo, this.WorkContext.CurrentUser);
                    #endregion

                    #region 给原图加水印
                    if (PreAdminConfigSettingService.IsWaterImage.CompareTo("1") == 0)
                    {
                        var sourceWaterImagePhysicalPath = Path.Combine(Server.MapPath(mapPath), PreAdminConfigSettingService.WaterImageNewFileNamePreName + newName);
                        var waterMarkPath = Server.MapPath(PreAdminConfigSettingService.WaterImagePath);
                        //图片水印
                        Infrastructure.Crosscutting.Comm.Service.PicService
                            .PictureWatermark(sourcePhysicalPath, sourceWaterImagePhysicalPath, waterMarkPath,
                            Infrastructure.Crosscutting.Comm.Service.PicWaterMarkPosition.BR, 100, 7);
                    }
                    if (PreAdminConfigSettingService.IsWaterText.CompareTo("1") == 0)
                    {
                        var sourceWaterWordPhysicalPath = Path.Combine(Server.MapPath(mapPath), PreAdminConfigSettingService.WaterTextNewFileNamePreName + newName);
                        //文字水印
                        Infrastructure.Crosscutting.Comm.Service.PicService.TextWatermark(sourcePhysicalPath,
                            sourceWaterWordPhysicalPath, PreAdminConfigSettingService.WaterTextName,
                        Infrastructure.Crosscutting.Comm.Service.PicWaterMarkPosition.BM);
                    }
                    //可以添加一张水印图片表
                    //将这些记录到 数据库，没有做这功能2012.5.8.15.43

                    #endregion

                    foreach (var thumb in picThumbSize)
                    {
                        var picSize = picThumbSizeService.Get(thumb);
                        if (picSize != null)
                        {
                            //生成缩略图
                            var thumbPath = Path.Combine(Server.MapPath(mapPath), picSize.ThumbWidth + "_" + picSize.ThumbHeight + "_" + newName);

                            //Infrastructure.Crosscutting.Comm.Service.PicService.BuilderThumbnail(sourcePhysicalPath, thumbPath,
                            //    picSize.ThumbWidth, picSize.ThumbHeight);

                            Infrastructure.Crosscutting.Comm.Service.PicService.BuilderThumbnail(sourcePhysicalPath, thumbPath,
                                picSize.ThumbWidth, picSize.ThumbHeight, Infrastructure.Crosscutting.Comm.Service.PicThumbnailType.CUT);

                            //添加到数据库中
                            #region add pic thumb to db

                            Domain.Entity.PicThumb tempThumb = new Domain.Entity.PicThumb();
                            tempThumb.AddTime = System.DateTime.Now;
                            tempThumb.AudTime = null;
                            tempThumb.DeleteTime = null;
                            tempThumb.Height = picSize.ThumbHeight;
                            //tempThumb.Id
                            tempThumb.IpAddress = Infrastructure.Crosscutting.Comm.Service.StringService.GetRealIP();
                            tempThumb.IsAud = true;
                            tempThumb.IsDelete = false;
                            tempThumb.IsTop = false;
                            tempThumb.IsVouch = false;
                            tempThumb.Name = picSize.ThumbWidth + "_" + picSize.ThumbHeight + "_" + newName;
                            tempThumb.PhyPrePath = mapPath.Substring(1);
                            tempThumb.PicId = picInfo.Id;
                            tempThumb.Remark = Path.GetFileNameWithoutExtension(sourceFileName);
                            tempThumb.SortNum = 0;
                            tempThumb.SourceName = sourceFileName;
                            tempThumb.State = true;
                            tempThumb.TopTime = null;
                            tempThumb.UserId = this.WorkContext.CurrentUser.Id;
                            tempThumb.UserName = this.WorkContext.CurrentUser.UserName;
                            tempThumb.ViewCount = 0;
                            tempThumb.VouchTime = null;
                            tempThumb.WebFullPath = mapPath.Substring(1) + picSize.ThumbWidth + "_" + picSize.ThumbHeight + "_" + newName;
                            tempThumb.WebPrePath = mapPath.Substring(1);
                            tempThumb.Width = picSize.ThumbWidth;
                            picThumbService.Add(tempThumb, this.WorkContext.CurrentUser);
                            #endregion

                            //生成缩略图，添加到数据库，没有做这功能2012.5.8.15.44

                            //生成缩略图，加图片水印
                            if (PreAdminConfigSettingService.IsThumbWaterImage.CompareTo("1") == 0)
                            {

                            }
                            //生成缩略图，加文字水印
                            if (PreAdminConfigSettingService.IsThumbWaterText.CompareTo("1") == 0)
                            {

                            }
                        }
                    }

                    //添加一条数据到对应的targetid 的pic表里面
                    //picInfo.Id
                    if (picType.IndexOf("景点") >= 0)
                    {
                        var sightPic = new Domain.Entity.SightPointPic();
                        sightPic.AddTime = System.DateTime.Now;
                        sightPic.IpAddress = Infrastructure.Crosscutting.Comm.Service.StringService.GetRealIP();
                        sightPic.PicInfoId = picInfo.Id;
                        sightPic.SightId = targetId;
                        sightPic.SortNum = 0;
                        sightPic.State = true;
                        sightPicService.Add(sightPic, this.WorkContext.CurrentUser);
                    }
                    else if (picType.IndexOf("商品") >= 0)
                    {
                        var goodsPic = new Domain.Entity.GoodsPic();
                        goodsPic.AddTime = System.DateTime.Now;
                        goodsPic.IpAddress = Infrastructure.Crosscutting.Comm.Service.StringService.GetRealIP();
                        goodsPic.PicInfoId = picInfo.Id;
                        goodsPic.GoodsId = targetId;
                        goodsPic.SortNum = 0;
                        goodsPic.State = true;
                        goodsPicService.Add(goodsPic, this.WorkContext.CurrentUser);
                    }
                    else if (picType.IndexOf("线路") >= 0)
                    {
                        var goodsPic = new Domain.Entity.TourPic();
                        goodsPic.AddTime = System.DateTime.Now;
                        goodsPic.IpAddress = Infrastructure.Crosscutting.Comm.Service.StringService.GetRealIP();
                        goodsPic.PicInfoId = picInfo.Id;
                        goodsPic.TourId = targetId;
                        goodsPic.SortNum = 0;
                        goodsPic.State = true;
                        tourPicService.Add(goodsPic, this.WorkContext.CurrentUser);
                    }
                    else if (picType.IndexOf("文章") >= 0)
                    {
                        var articlePic = new Domain.Entity.ArticlePic();
                        articlePic.AddTime = System.DateTime.Now;
                        articlePic.IpAddress = Infrastructure.Crosscutting.Comm.Service.StringService.GetRealIP();
                        articlePic.PicInfoId = picInfo.Id;
                        articlePic.ArticleId = targetId;
                        articlePic.SortNum = 0;
                        articlePic.State = true;
                        articlePicService.Add(articlePic, this.WorkContext.CurrentUser);
                    }
                    else if (picType.IndexOf("广告") >= 0)
                    {
                        var advertisePic = new Domain.Entity.AdvertisePic();
                        advertisePic.AddTime = System.DateTime.Now;
                        advertisePic.IpAddress = Infrastructure.Crosscutting.Comm.Service.StringService.GetRealIP();
                        advertisePic.PicInfoId = picInfo.Id;
                        advertisePic.AdverseId = targetId;
                        advertisePic.SortNum = 0;
                        advertisePic.State = true;
                        adPicService.Add(advertisePic, this.WorkContext.CurrentUser);
                    }
                }
                res = true;
            }
            catch (Exception ex)
            {
                message = ex.Message;
            }
        }

        protected void PicDeleteBase(int id, int targetId, string type)
        {
            if (type.Trim().CompareTo("景点") == 0)
            {
                var sightDelete = sightPicService.GetList()
                    .Where(e => e.PicInfoId == id && e.SightId == targetId).FirstOrDefault();
                sightPicService.Delete(sightDelete, this.WorkContext.CurrentUser);
            }
            else if (type.Trim().CompareTo("线路") == 0)
            {
                var tourDelete = tourPicService.GetList()
                        .Where(e => e.PicInfoId == id && e.TourId == targetId).FirstOrDefault();
                tourPicService.Delete(tourDelete, this.WorkContext.CurrentUser);
            }
            else if (type.Trim().CompareTo("商品") == 0)
            {
                var goodsDelete = goodsPicService.GetList()
                        .Where(e => e.PicInfoId == id && e.GoodsId == targetId).FirstOrDefault();
                goodsPicService.Delete(goodsDelete, this.WorkContext.CurrentUser);
            }
            else if (type.Trim().CompareTo("文章") == 0)
            {
                var articleDelete = articlePicService.GetList()
                        .Where(e => e.PicInfoId == id && e.ArticleId == targetId).FirstOrDefault();
                articlePicService.Delete(articleDelete, this.WorkContext.CurrentUser);
            }
        }


        protected void PicDeleteTrueBase(int id, int targetId, string type)
        {
            if (type.Trim().CompareTo("景点") == 0)
            {
                var sightDelete = sightPicService.GetList()
                    .Where(e => e.PicInfoId == id && e.SightId == targetId).FirstOrDefault();
                sightPicService.DeleteTrue(sightDelete, this.WorkContext.CurrentUser);
            }
            else if (type.Trim().CompareTo("线路") == 0)
            {
                var tourDelete = tourPicService.GetList()
                        .Where(e => e.PicInfoId == id && e.TourId == targetId).FirstOrDefault();
                tourPicService.DeleteTrue(tourDelete, this.WorkContext.CurrentUser);
            }
            else if (type.Trim().CompareTo("商品") == 0)
            {
                var goodsDelete = goodsPicService.GetList()
                        .Where(e => e.PicInfoId == id && e.GoodsId == targetId).FirstOrDefault();
                goodsPicService.DeleteTrue(goodsDelete, this.WorkContext.CurrentUser);
            }
            else if (type.Trim().CompareTo("文章") == 0)
            {
                var articleDelete = articlePicService.GetList()
                        .Where(e => e.PicInfoId == id && e.ArticleId == targetId).FirstOrDefault();
                articlePicService.DeleteTrue(articleDelete, this.WorkContext.CurrentUser);
            }
            else if (type.Trim().CompareTo("广告") == 0)
            {
                var advertiseDelete = adPicService.GetList()
                        .Where(e => e.PicInfoId == id && e.AdverseId == targetId).FirstOrDefault();
                adPicService.DeleteTrue(advertiseDelete, this.WorkContext.CurrentUser);
            }
        }

        protected IQueryable<Domain.Entity.PicInfo> GetPicTrueState(string type, IQueryable<Domain.Entity.PicInfo> data, int targetId)
        {
            if (type.Trim().CompareTo("景点") == 0)
            {
                var sightPicId = sightPicService.GetList()
                    .Where(e => e.SightId == targetId).Select(e => e.PicInfoId);
                data = data.Where(e => sightPicId.Contains(e.Id));
            }
            else if (type.Trim().CompareTo("线路") == 0)
            {
                var tourPicId = tourPicService.GetList()
                    .Where(e => e.TourId == targetId).Select(e => e.PicInfoId);
                data = data.Where(e => tourPicId.Contains(e.Id));
            }
            else if (type.Trim().CompareTo("商品") == 0)
            {
                var goodsPicId = goodsPicService.GetList()
                    .Where(e => e.GoodsId == targetId).Select(e => e.PicInfoId);
                data = data.Where(e => goodsPicId.Contains(e.Id));
            }
            else if (type.Trim().CompareTo("文章") == 0)
            {
                var articlePicId = articlePicService.GetList()
                    .Where(e => e.ArticleId == targetId).Select(e => e.PicInfoId);
                data = data.Where(e => articlePicId.Contains(e.Id));
            }
            else if (type.Trim().CompareTo("广告") == 0)
            {
                var advertisePicId = adPicService.GetList()
                    .Where(e => e.AdverseId == targetId).Select(e => e.PicInfoId);
                data = data.Where(e => advertisePicId.Contains(e.Id));
            }
            return data;
        }

        protected string GetPicSaveMapPath(string picType)
        {
            var mapPath = PreAdminConfigSettingService.PicSavePath + "default/";
            if (!string.IsNullOrEmpty(picType))
            {
                if (picType.IndexOf("景点") >= 0)
                {
                    mapPath = PreAdminConfigSettingService.PicSavePath + "sight/";
                }
                else if (picType.IndexOf("商品") >= 0)
                {
                    mapPath = PreAdminConfigSettingService.PicSavePath + "goods/";
                }
                else if (picType.IndexOf("线路") >= 0)
                {
                    mapPath = PreAdminConfigSettingService.PicSavePath + "tour/";
                }
                else if (picType.IndexOf("文章") >= 0)
                {
                    mapPath = PreAdminConfigSettingService.PicSavePath + "article/";
                }
                else if (picType.IndexOf("广告") >= 0)
                {
                    mapPath = PreAdminConfigSettingService.PicSavePath + "advertise/";
                }
            }
            return mapPath;
        }

        protected IQueryable<Domain.Entity.PicInfo> GetPicTrueState()
        {
            var data = picInfoService.GetList().Where(e => e.State == true)
                .OrderByDescending(e => e.Id);
            return data;
        }

        protected IQueryable<Domain.Entity.PicClass> GetPicClassTrueState()
        {
            var data = picClassService.GetList().Where(e => e.State == true)
                .OrderByDescending(e => e.Id);
            return data;
        }

        protected IQueryable<Domain.Entity.PicThumbSize> GetPicThumbSizeTrueState()
        {
            var data = picThumbSizeService.GetList().Where(e => e.State == true)
                .OrderByDescending(e => e.Id);
            return data;
        }
    }
}
